﻿using System.Xml;

namespace System.Management.HyperV
{
    /// <summary>
    /// Provides detailed information about an existing virtual hard disk (VHD) image.
    /// </summary>
    /// <remarks>http://msdn.microsoft.com/en-us/library/cc136935(VS.85).aspx</remarks>
    public class VirtualHardDiskInfo
    {
        #region Constructor
        internal VirtualHardDiskInfo(string xml)
        {
            var doc = new XmlDocument();
            doc.LoadXml(xml);

            string xpathTemplate = @"//PROPERTY[@NAME = '{0}']/VALUE/child::text()";

            this.DiskType            = (VirtualHardDiskType)ushort.Parse(doc.SelectSingleNode(string.Format(xpathTemplate, "Type")).Value);

            this.FileSize        = ulong .Parse(doc.SelectSingleNode(string.Format(xpathTemplate, "FileSize")).Value);
            this.MaxInternalSize = ulong .Parse(doc.SelectSingleNode(string.Format(xpathTemplate, "MaxInternalSize")).Value);
            this.InSavedState    = bool  .Parse(doc.SelectSingleNode(string.Format(xpathTemplate, "InSavedState")).Value);
            this.InUse           = bool  .Parse(doc.SelectSingleNode(string.Format(xpathTemplate, "InUse")).Value);

            var nodeParentPath = doc.SelectSingleNode(string.Format(xpathTemplate, "ParentPath"));
            this.ParentPath = (nodeParentPath != null) ? nodeParentPath.InnerText : string.Empty;
        }
        #endregion

        public VirtualHardDiskType DiskType { get; private set; }
        public ulong FileSize { get; private set; }
        public ulong MaxInternalSize { get; private set; }
        public bool InSavedState { get; private set; }
        public bool InUse { get; private set; }
        public string ParentPath { get; private set; }
    }
}
